//
//  LSZChangePasswordVC.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/19.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZChangePasswordVC.h"

@interface LSZChangePasswordVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray * array;
}
@property(nonatomic,strong)UITableView * tableViewChangePassword;
@end

@implementation LSZChangePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    
    
    [self.tableViewChangePassword registerClass:[LSZChangePasswordTableViewCell class] forCellReuseIdentifier:@"LSZChangePasswordTableViewCell"];
    [self.tableViewChangePassword registerClass:[LSZPasswordFootCell class] forHeaderFooterViewReuseIdentifier:@"LSZPasswordFootCell"];
    array = @[@"原密码",@"新密码",@"确认密码"];
    UITapGestureRecognizer *click = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAction)];
    [self.view addGestureRecognizer:click];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else{
        return 1;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section== 0) {
        return 45;
    }else{
        return 0.001;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 20;
    }else{
        return 45;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"cell"];
        }
        return cell;
    }else{
        LSZPasswordFootCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZPasswordFootCell"];
        [cell.butOk addTarget:self action:@selector(clickBut) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        LSZChangePasswordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZChangePasswordTableViewCell" forIndexPath:indexPath];
        cell.labelTitle.text = [NSString stringWithFormat:@"%@",array[indexPath.row]];
        cell.textFieldContent.placeholder = [NSString stringWithFormat:@"请输入%@",array[indexPath.row]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LSZPasswordFootCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
#pragma mark - 立即修改密码
-(void)clickBut
{
    LSZChangePasswordTableViewCell * cellOne = (LSZChangePasswordTableViewCell *)[self.tableViewChangePassword cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:0]];
    LSZChangePasswordTableViewCell * cellTwo = (LSZChangePasswordTableViewCell *)[self.tableViewChangePassword cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1  inSection:0]];
    LSZChangePasswordTableViewCell * cellThree = (LSZChangePasswordTableViewCell *)[self.tableViewChangePassword cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2  inSection:0]];
    NSLog(@"%@>>>>%@>>>>>%@",cellOne.textFieldContent.text,cellTwo.textFieldContent.text,cellThree.textFieldContent.text);
    if(![cellTwo.textFieldContent.text isEqualToString:cellThree.textFieldContent.text]){
        [self initWithtring:@"新密码输入不一致!"];
        return;
    }
    [LSZRequestData initWithChangeThePassword:cellOne.textFieldContent.text :cellTwo.textFieldContent.text :^(BOOL isOk, NSString * string, NSString * str) {
        if(isOk == YES){
            [self initWithtring:@"密码修改成功!"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [self initWithtring:string];
        }
    }];
}
-(void)clickAction
{
    [self.view endEditing:YES];
}
-(UITableView *)tableViewChangePassword
{
    if (!_tableViewChangePassword) {
        _tableViewChangePassword = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
        _tableViewChangePassword.delegate = self;
        _tableViewChangePassword.dataSource = self;
        _tableViewChangePassword.estimatedRowHeight=0;
        _tableViewChangePassword.estimatedSectionFooterHeight = 0;
        _tableViewChangePassword.estimatedSectionHeaderHeight =0;
        [_tableViewChangePassword setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
        [self.view addSubview:_tableViewChangePassword];
    }
    return _tableViewChangePassword;
}

@end
